package me.mystream.streamdemo;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamDemo8 {
    public static void main(String[] args) {
        //collect方法：收集流中的数据，放到集合中
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌-男-18", "赵敏-女-19", "张强-男-20", "周芷若-女-21");

        //收集到List集合中
        //要求把所有男性收集起来
        List<String> newList = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toList());
        //System.out.println(newList);

        //收集到Set集合中
        //要求把所有男性收集起来
        Set<String> newSet = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toSet());
        //System.out.println(newSet);

        //收集到Map集合中
        //键是姓名，值是年龄
        //Map集合中键不能重复
        Map<String, Integer> newMap1 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split("-")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split("-")[2]);
                    }
                }));
        System.out.println(newMap1);

        //简化
        Map<String, Integer> newMap2 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(s -> s.split("-")[0], s -> Integer.parseInt(s.split("-")[2])));
        System.out.println(newMap2);
    }
}
